package com.qst.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.pojo.Houselist;
import com.qst.pojo.User;
import com.qst.service.HouselistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller

public class HoustlistController {
	 @Autowired private HouselistService houselistService;

	 @RequestMapping("/houselist") public String houseList(Model model,
			 @RequestParam(required = false, defaultValue = "1") Integer page,
			 @RequestParam(required = false, defaultValue = "10") Integer pageSize,
			 HttpSession httpSession, @RequestParam(defaultValue = "未租赁") String status) {
		  User user=(User) httpSession.getAttribute("user");
		  PageHelper.startPage(page, pageSize);
		  List<Houselist> houselist = houselistService.selectAll(status);
		  PageInfo<Houselist> p = new PageInfo<Houselist>(houselist);
		  model.addAttribute("p", p);
		  model.addAttribute("houselist", houselist);
		  model.addAttribute("mainPage", "../house/houselist.jsp");
		  return "common/main";
	 }
	 @RequestMapping("/myhouselist") public String myhouselist(Model model,
			 @RequestParam(required = false, defaultValue = "1") Integer page,
			 @RequestParam(required = false, defaultValue = "10") Integer pageSize,
			 HttpSession httpSession, @RequestParam(defaultValue = "未租赁") String status) {
		  User user=(User) httpSession.getAttribute("user");

		  PageHelper.startPage(page, pageSize);
		  List<Houselist> houselist = houselistService.selectAllByUserId(user.getId(), status);
		  PageInfo<Houselist> p = new PageInfo<Houselist>(houselist);
		  model.addAttribute("p", p);
		  model.addAttribute("houselist", houselist);
		  model.addAttribute("mainPage", "../house/houselist.jsp");
		  return "common/main";
	 }

	 @RequestMapping("/addhouse") public String addhouse(Model model, Houselist houselist) {

		  String houseid = houselist.getHouseid();
		  Houselist houselist1 = houselistService.findhouseid(houseid);
		  if (houselist1 != null) {
				model.addAttribute("error", "该房屋编号已存在");
				model.addAttribute("houselist", houselist);
				model.addAttribute("mainPage", "../house/addhouse.jsp");
				return "common/main";
		  } else {
				houselist.setCreatedate(new Date());
				houselistService.inserthouse(houselist);
				model.addAttribute("error", "添加成功");
				model.addAttribute("houselist", houselist);
				model.addAttribute("mainPage", "../house/addhouse.jsp");
				return "common/main";
		  }
	 }

	 @RequestMapping("/toaddhouse") public String toaddhoust(Model model) {
		  model.addAttribute("mainPage", "../house/addhouse.jsp");
		  return "common/main";
	 }


	 @RequestMapping("/toahouselist") public String toahouselist() {

		  return "houselist.action";
	 }

	 /**
	  * 打开更新房屋表单
	  */
	 @RequestMapping("/houselist/updateform") public String findid(Integer id, Model model) {
		  Houselist list = houselistService.findid(id);
		  model.addAttribute("houselist", list);
		  model.addAttribute("mainPage", "../house/houseupdate.jsp");
		  return "common/main";
	 }

	 /**
	  * 删除房源
	  */
	 @RequestMapping("/houselist/delete") public String deletehouse(Integer id,Model model,HttpSession httpSession) {
		  User user=(User) httpSession.getAttribute("user");
		  houselistService.deletehouse(id);
		  model.addAttribute("error", "delete");
		  String url ="redirect:../houselist.action";
		  if ("fangdong".equals(user.getUsertype())){
				url="redirect:../myhouselist.action";
		  }
		  return url;
	 }

	 /**
	  * 执行更新操作
	  */
	 @RequestMapping("/houselist/update") public String findhouseidupdate(Houselist houselist,
			 Model model) {
		  Houselist list = houselistService.findhouseidupdate(houselist);
		  if (list != null) {
				model.addAttribute("houselist", houselist);
				model.addAttribute("mainPage", "../house/houseupdate.jsp");
				model.addAttribute("error", "该房屋id已存在");
				return  "common/main";
		  } else {
				houselistService.updatehouse(houselist);
				model.addAttribute("houselist", houselist);
				model.addAttribute("mainPage", "../house/houseupdate.jsp");
				model.addAttribute("error", "更新成功");
				return  "common/main";
		  }
	 }

}
